const util = require("../utils/connection");

module.exports = {
  getUserById: function(id) {
    return new Promise(async (resolve, reject) => {
      const connection = await util.getConnection();
      // connection.beginTransaction((error) => {
      //   if (error) throw error;
      // 开启查询
      connection.query(
        `select username, address, sex, createTime, updateTime from user where id = ${id}`,
        function(error, result) {
          connection.release();
          if (error) {
            reject({ message: error.message });
            return;
          }
          resolve({ data: result[0] || {} });
        }
      );
    });
    // });
  },
  getUserList: function() {
    return new Promise(async (resolve, reject) => {
      const connection = await util.getConnection();
      connection.query(
        "select username, address, sex, createTime, updateTime from user",
        function(error, result) {
          connection.release();
          if (error) {
            reject({ message: error.message });
            return;
          }
          resolve({ data: result || [] });
        }
      );
    });
  }
};
